package com.imooc.diners.controller;

import com.imooc.commons.model.domain.ResultInfo;
import com.imooc.commons.utils.ResultInfoUtil;
import com.imooc.diners.service.SignService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 签到控制层
 *
 * @author gcq
 * @Create 2022-11-26
 */
@RestController
@RequestMapping("sign")
public class SignController {

    @Resource
    private SignService signService;
    @Resource
    private HttpServletRequest request;

    /**
     * 获取对应月份签到信息
     *
     * @param access_token
     * @param date
     * @return
     */
    @GetMapping("info")
    public ResultInfo getSignInfo(String access_token, String date){
        Map<String, Boolean> signInfo = signService.getSignInfo(access_token, date);
        return ResultInfoUtil.buildSuccess(request.getServletPath(), signInfo);
    }

    /**
     * 获取签到次数，默认当月
     *
     * @param access_token
     * @param date
     * @return
     */
    @GetMapping("count")
    public ResultInfo getSignCount(String access_token, String date){
        Long count = signService.getSignCount(access_token, date);
        return ResultInfoUtil.buildSuccess(request.getServletPath(), count);
    }

    /**
     * 签到,可以补签
     *
     * @param access_token
     * @param date
     * @return
     */
    @PostMapping()
    public ResultInfo sign(String access_token,
                           @RequestParam(required = false) String date){
        int count = signService.doSing(access_token, date);
        return ResultInfoUtil.buildSuccess(request.getServletPath(), count);
    }


    // TODO 获取用户连续签到天数
}